using System;
using UnityEngine;
/// <summary>
/// MonoBehaviour单例
/// </summary>
/// <typeparam name="T">类型</typeparam>
public abstract class AbsHFSingleMonoBehaviour<T> : AbsHFSimulateBehaviour
        where T : AbsHFSingleMonoBehaviour<T>, new()
{
    /// <summary>
    /// 实例
    /// </summary>
    static T mInstance = null;
    /// <summary>
    /// 实例
    /// </summary>
    public static T Instance
    {
        get
        {
            if (mInstance == null)
            {
                mInstance = new T();
                GameObject go = new GameObject(typeof(T).Name);                
                GameObject.DontDestroyOnLoad(go);
                mInstance.BindGameObject(go);
                mInstance.OnAfterCreateInstance();
                HFSingleMonoBehaviourManager.AddSingle(mInstance);
            }
            return mInstance;
        }
    }

    /// <summary>
    /// 在创建实例后
    /// </summary>
    protected virtual void OnAfterCreateInstance() { }
}
